package cn.jmz.micpro.controller.handler;

import cn.jmz.micpro.common.enums.BaseEnum;
import cn.jmz.micpro.common.exception.DefinitionException;
import cn.jmz.micpro.common.result.GlobalResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     *
     */
    @ExceptionHandler(value = DefinitionException.class)
    @ResponseBody
    public GlobalResult bizExceptionHandler(DefinitionException e) {
        return GlobalResult.defineError(e);
    }

    /**
     * 处理其他异常
     *
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public GlobalResult exceptionHandler( Exception e) {
        return new GlobalResult(BaseEnum.ERROR.getCode(),e.getMessage());
    }
}
